import json
import os

import xlrd
import yaml


def analyze_file(file_name, data_key):
    with open("." + os.sep + "data" + os.sep + file_name, "r", encoding="utf-8") as f:
        data = yaml.load(f)
        dict_data = data[data_key]
        list_data = list()
        for i in dict_data.values():
            list_data.append(i)
        # print(list_data)

        return list_data


def analyze_json(file_name, data_key):
    with open("." + os.sep + "data" + os.sep + file_name, "r") as f:
        data = json.load(f)
        return data[data_key]

def analyze_txt(file_name):
    with open("." + os.sep + "data" + os.sep + file_name, "r") as f:
        data_list = f.read().split("\n")
        data_param = []
        for i in data_list:
            data_param.append(tuple(i.split(",")))
        print(data_param)

def analyze_xls(file_name, sheet_name):
    xls_file = xlrd.open_workbook("." + os.sep + "data" + os.sep + file_name)

    # 获取表格的sheet
    sheet = xls_file.sheet_by_name(sheet_name)

    # 获取一行元素个数rows_num的和一列元素个数cols_num
    keys_name = sheet.row_values(0)
    rows_num = len(keys_name)
    cols_num = len(sheet.col_values(0))

    data_result = list()

    for i in range(1, cols_num):
        dict_tmp = dict()
        for j in range(rows_num):
            dict_tmp[keys_name[j]] = sheet.cell(i, j).value

        data_result.append(dict_tmp)
    # print(data_result)
    return data_result

# if __name__ == '__main__':
#     analyze_xls("cases.xls", "login")